package com.example.iceman.stepcounterapp;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;

/**
 * Created by iceman on 16/2/15.
 */
public class NewStepCounter implements SensorEventListener {
    private final Context mContext;
    public int monitorInterval = 20;
    public int filterTime = 300;
    private SensorManager sensorManager;
    private Sensor acceSensor;
    private final String TAG = "NewStepCounter";
    private float[] accelerometerValues;
    private StepChangeListener onStepListener;
    private StepCalcu stepCalcu;

    private boolean logON;

    private int sensorDataAverageNum;
    //    private Sensor oritationSensor;
    private int currentZOritation = 0;

    public NewStepCounter(Context context) {
        this(context, new StepCountConfig());
    }

    public static class StepCountConfig {
        public int monitorInterval = 20;
        public int filterTime = 300;
        public boolean logOn = false;
    }

    /**
     * 计步器主体类
     *
     * @param context
     * @param monitorInterval 传感器检测间隔
     * @param filterTime      步伐间隔时间,用来限制两步之间最短时间间隔
     */
    public NewStepCounter(Context context, StepCountConfig config) {
        mContext = context;
        initParams(config);
        initSensor();
    }

    private void initParams(StepCountConfig config) {
        if (config.monitorInterval > 100) {
            config.monitorInterval = 100;
        } else if (monitorInterval < 10) {
            config.monitorInterval = 20;
        }
        sensorDataAverageNum = 100 / config.monitorInterval;
        this.monitorInterval = config.monitorInterval * 1000;
        this.filterTime = config.filterTime;
        this.logON = config.logOn;
    }

    private void initSensor() {
        sensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
        if (sensorManager != null) {
//            oritationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
            acceSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            if (acceSensor == null) {
                Log.e(TAG, "加速度传感器获取失败，无法计步");
            }
        } else {
            Log.e(TAG, "传感器初始化失败");
        }
    }

    /**
     * 开始计数器
     */
    public void start() {
        stepCalcu = new StepCalcu(this, filterTime);
        if (acceSensor != null) {
            sensorManager.registerListener(this, acceSensor, monitorInterval);
//            sensorManager.registerListener(this, oritationSensor, monitorInterval);

        }
    }

    /**
     * 停止计步
     */
    public void stop() {
        if (sensorManager != null) {
            sensorManager.unregisterListener(this);
        }
    }

    int index = 0;
    float[] temparr = new float[5];

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
//            Log.i(TAG, "方向:" + event.values[0] + ":" + event.values[1] + ":" + event.values[2]);
//            currentZOritation = (int) event.values[0];
        } else if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

            accelerometerValues = event.values.clone();
            float gravityNew = (float) Math.sqrt(accelerometerValues[0] * accelerometerValues[0]
                    + accelerometerValues[1] * accelerometerValues[1] + accelerometerValues[2] * accelerometerValues[2]);
            temparr[index] = gravityNew;
            index++;
            if (index == sensorDataAverageNum) {
                index = 0;
                saveAccelerometer();
            }
        }
    }

    private void saveAccelerometer() {
        float sum = 0;
        for (int i = 0; i < sensorDataAverageNum; i++) {
            sum += temparr[i];
        }
        float ave = sum / sensorDataAverageNum;
        if (stepCalcu != null) {
            stepCalcu.DetectorNewStep(ave);
        }
    }

    public void onLog(String s) {
        if (logON) {
            Log.d(TAG, s);
            if (onStepListener != null) {
                onStepListener.onLog(s);
            }
        }
    }

    public void onStep() {
        if (onStepListener != null) {
            onStepListener.onStep(currentZOritation);
        }
    }

    public void setOnStepListener(StepChangeListener listener) {
        onStepListener = listener;
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}
